package com.mt.mtbusinessmanagementsystem.controller;

import com.github.pagehelper.PageInfo;
import com.mt.mtbusinessmanagementsystem.constant.GeneralConstant;
import com.mt.mtbusinessmanagementsystem.constant.MessageConstant;
import com.mt.mtbusinessmanagementsystem.pojo.dto.CustomerDTO;
import com.mt.mtbusinessmanagementsystem.pojo.dto.DeptDTO;
import com.mt.mtbusinessmanagementsystem.pojo.dto.EmployeeDTO;
import com.mt.mtbusinessmanagementsystem.pojo.dto.LoginDTO;
import com.mt.mtbusinessmanagementsystem.pojo.entity.Customer;
import com.mt.mtbusinessmanagementsystem.pojo.entity.Dept;
import com.mt.mtbusinessmanagementsystem.pojo.entity.Employee;
import com.mt.mtbusinessmanagementsystem.pojo.entity.FollowMethod;
import com.mt.mtbusinessmanagementsystem.service.GeneralService;
import com.mt.mtbusinessmanagementsystem.utils.IsTrue;
import com.mt.mtbusinessmanagementsystem.utils.Result;
import com.mt.mtbusinessmanagementsystem.utils.entry.Entry;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;

@RestController
@Slf4j
@CrossOrigin
@RequestMapping("/general")
public class GeneralController {
    @Autowired
    private GeneralService generalService;

    /**
     * 实现登录方法
     * @param employee
     * @return
     */
    @PostMapping("/login")
    public Result<LoginDTO> login(@RequestBody Employee employee){
        log.info("目前进行登录操作, 接受到的数据有: {}",employee);
        IsTrue<LoginDTO> isTrue=generalService.login(employee);
        return isTrue.getIsTrue()?Result.success(isTrue.getData()):Result.error(MessageConstant.LOGIN_ILLEGAL);
    }

    /**
     * 增加部门
     * @param dept
     * @return
     */
    @PostMapping("/dept/increase")
    public Result<String> addDept(@RequestBody Dept dept){
        log.info("现在进行增加部门操作, 当前接受到的数据有: {}",dept);
        IsTrue<String> isTrue=generalService.addDept(dept);
        return isTrue.getIsTrue()?Result.success(MessageConstant.DEPT_ADD_SUCCESSFUL):Result.error(isTrue.getStr());
    }

    /**
     * 现在进行删除部门操作,并将员工转移到未分配
     * @param deptId
     * @return
     */
    @DeleteMapping("/dept/delete")
    public Result<String> delDept(@RequestParam Integer deptId){
        log.info("现在进行删除部门操作, 接收到的数据有: {}",deptId);
        IsTrue<String> isTrue=generalService.delDept(deptId);
        return isTrue.getIsTrue()?Result.success(MessageConstant.DEPT_DEL_SUCCESSFUL):Result.error(isTrue.getStr());
    }
    /**
     * 修改部门信息,可以更换主管
     * @param dept
     * @return
     */
    @PutMapping("/dept/update")
    public Result<Dept> updateDept(@RequestBody Dept dept){
        log.info("现在进行修改部门信息操作, 接收到的数据有: {}",dept);
        Dept newDept=generalService.updateDept(dept);
        return Result.success(newDept);
    }

    /**
     * 查询部门信息
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @GetMapping("/dept/list")
    public Result<PageInfo<DeptDTO>> deptInfoByList(@RequestParam Integer pageNumber, @RequestParam Integer pageSize){
        log.info("现在进行查询部门信息操作, 接收到的信息有: {} {}",pageNumber,pageSize);
        PageInfo<DeptDTO> pageInfo=generalService.deptInfoByList(pageNumber,pageSize);
        return Result.success(pageInfo);
    }

    /**
     * 通过部门号查询员工
     * @param deptId
     * @return
     */
    @GetMapping("/dept/emp")
    public Result<ArrayList<Employee>> employeeByDept(@RequestParam Integer deptId){
        log.info("通过部门号查询员工, 接收到的信息有: {}",deptId);
        ArrayList<Employee> list=generalService.employeeByDept(deptId);
        return Result.success(list);
    }

    /**
     * 添加员工
     * @param employeeDTO
     * @return
     */
    @PostMapping("/employee/increase")
    public Result<String> addEmployee(@RequestBody EmployeeDTO employeeDTO){
        log.info("现在进行添加员工操作, 接收到的信息有: {}",employeeDTO);
        IsTrue<String> isTrue=generalService.addEmployee(employeeDTO);
        return isTrue.getIsTrue()?Result.success(MessageConstant.EMPLOYEE_ADD_SUCCESSFUL):Result.error(isTrue.getStr());
    }

    /**
     * 修改员工信息
     * @param employee
     * @return
     */
    @PutMapping("/employee/put")
    public Result putEmployee(@RequestBody Employee employee){
        log.info("现在进行修改员工信息操作, 接收到的信息有: {}",employee);
        generalService.putEmployee(employee);
        return Result.success(MessageConstant.UPDATED_SUCCESSFUL);
    }

    /**
     * 修改员工头像
     * @param employeeId
     * @param headFile
     * @return
     */
    @PostMapping("/employee/head/put")
    public Result<String> updateHead(@RequestParam String employeeId,@RequestParam MultipartFile headFile){
        log.info("现在进行修改员工头像的操作,接收到的信息分别是: {} {}",employeeId,headFile);
        String url=generalService.putEmployeeHead(employeeId,headFile);
        return Result.success(url);
    }

    /**
     * 员工列表
     * @param employeeDTO
     * @return
     */
    @GetMapping("/employee/list")
    public Result<PageInfo<EmployeeDTO>> queryEmployeeList(@RequestBody EmployeeDTO employeeDTO){
        log.info("现在进行查询员工列表功能, 接收到信息是: {}",employeeDTO);
        PageInfo<EmployeeDTO> pageInfo=generalService.queryEmployeeList(employeeDTO);
        return Result.success(pageInfo);
    }

    /**
     * 添加客户,如果有公司名会添加公司
     * @param customerDTO
     * @return
     */
    @PostMapping("/customer/increase")
    public Result<CustomerDTO> addCustomer(@RequestBody CustomerDTO customerDTO){
        log.info("现在进行添加客户操作, 信息分别是: {}",customerDTO);
        IsTrue<CustomerDTO> isTrue=generalService.addCustomer(customerDTO);
        return isTrue.getIsTrue()?Result.success(isTrue.getData()):Result.error(isTrue.getStr());
    }

    /**
     * 根据条件查询客户
     * @param customerDTO
     * @return
     */
    @GetMapping("/customer/list")
    public Result<PageInfo<CustomerDTO>> queryCustomerList(@RequestBody CustomerDTO customerDTO){
        log.info("现在进行条件查询客户, 接收到的信息分别是: {}",customerDTO);
        PageInfo<CustomerDTO> pageInfo=generalService.queryCustomerList(customerDTO);
        return Result.success(pageInfo);
    }

    /**
     * 返回查询客户界面查询条件
     * @return
     */
    @GetMapping("/customer/query/condition")
    public Result<Entry<Entry<String,ArrayList<CustomerDTO>>,Entry<String,ArrayList<CustomerDTO>>>> queryCreateAndResponsibleInfo(){
        log.info("返回查询客户界面查询条件");
        Entry<Entry<String,ArrayList<CustomerDTO>>,Entry<String,ArrayList<CustomerDTO>>> entry=generalService.queryCreateAndResponsibleInfo();
        return Result.success(entry);
    }

    /**
     * 根据部门名查询员工
     * @param deptName
     * @return
     */
    @GetMapping("/employee/dept/name")
    public Result<ArrayList<Employee>> employeeByDeptName(@RequestParam String deptName){
        log.info("根据部门名查询员工, 接收到的信息有: {}",deptName);
        ArrayList<Employee> list=generalService.employeeByDeptName(deptName);
        return Result.success(list);
    }

    /**
     * 修改客户信息
     * @param customer
     * @return
     */
    @PutMapping("/customer/put")
    public Result<String> customerPut(@RequestBody Customer customer){
        log.info("现在进行修改客户信息, 接收到的信息有: {}",customer);
        generalService.customerPut(customer);
        return Result.success(MessageConstant.UPDATED_SUCCESSFUL);
    }

//    /** 暂时性不用
//     * 删除客户信息
//     * @return
//     */
//    @DeleteMapping("/customer/delete")
//    public Result delCustomer(@RequestBody CustomerDTO customerDTO){
//        log.info("现在继续删除客户操作,接收到的信息是: {}",customerDTO);
//        IsTrue<String> isTrue=generalService.delCustomer(customerDTO);
//        return isTrue.getIsTrue()?Result.success(String.format(MessageConstant.DATA_DELETE_SUCCESSFUL,
//                customerDTO.getCustomerId()!=null?customerDTO.getCustomerId():customerDTO.getId())):Result.error(isTrue.getStr());
//    }

    /**
     * 查看客户详细信息
     * @param customerDTO
     * @return
     */
    @GetMapping("/customer/detailed/info")
    public Result<CustomerDTO> customerDetailedInfo(@RequestBody CustomerDTO customerDTO){
        log.info("查看客户详细信息,信息分别是: {}",customerDTO);
        IsTrue<CustomerDTO> isTrue=generalService.customerDetailedInfo(customerDTO);
        return isTrue.getIsTrue()?Result.success(isTrue.getData()):Result.error(isTrue.getStr());
    }

    /**
     * 添加跟进方式
     * @param followMethod
     * @return
     */
    @PostMapping("/follow/method/increase")
    public Result<String> addFollowMethod(@RequestBody FollowMethod followMethod){
        log.info("现在进行添加跟进方式操作, 接收到的信息有: {}",followMethod);
        IsTrue isTrue=generalService.addFollowMethod(followMethod);
        return isTrue.getIsTrue()?Result.success(MessageConstant.DATA_INSERT_SUCCESSFUL):Result.error(isTrue.getStr());
    }

    /**
     * 删除跟进方式
     * @param followMethodId
     * @return
     */
    @DeleteMapping("/follow/method/delete")
    public Result<String> delFollowMethod(@RequestParam Integer followMethodId){
        log.info("现在进行删除跟进方式数据操作,接收到的信息有: {}",followMethodId);
        IsTrue<String> isTrue=generalService.delFollowMethod(followMethodId);
        return isTrue.getIsTrue()?Result.success(String.format(MessageConstant.DATA_DELETE_SUCCESSFUL, GeneralConstant.FOLLOW_METHOD_ID)):Result.error(isTrue.getStr());
    }


}
